package model.dao;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import model.bean.Language;
import model.common.Common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GetLanguageDAO {
	
	/**
	 * Get danh sách ngôn ngữ trong cơ sở dữ liệu
	 * @return
	 */
	public ArrayList<Language> GetListLang() {
		Common.connect();
		ArrayList<Language> listLang = new ArrayList<Language>();

		try {
			String sql = "select * from tblLanguages";

			Statement st = Common.conn.createStatement();
			ResultSet rss = st.executeQuery(sql);
			while (rss.next()) {
				Language language = new Language();
				String langId = rss.getString(1);
				String langName = rss.getString(2);
				language.setLangId(langId);
				language.setLangName(langName);
				listLang.add(language);
			}

		} catch (SQLException e) {
			System.err.println("loi thuc hien lay danh sách ngôn ngữ");
		} finally {
			Common.disconnect();
		}

		return listLang;
	}
	
	/**
	 * Lấy danh sách ngôn ngữ được hỗ trợ
	 * @return
	 */
	public ArrayList<Language> GetListLanguage(){
		ArrayList<Language> listLang = new ArrayList<Language>();
		String xmlFilePath = "F:\\Eclipse_Project\\MathDict\\WebContent\\Dictionary\\Dictionary.xml";
		try {
			File xmlFile = new File(xmlFilePath);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("languages");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					Language language = new Language();
					language.setLangId(eElement.getElementsByTagName("langId").item(0).getTextContent());
					language.setLangName(eElement.getElementsByTagName("langName").item(0).getTextContent());
					listLang.add(language);
				}
			}
		} catch (Exception e) {
            e.printStackTrace();
        }
		return listLang;
	}
	
	/**
	 * Get danh sách từ trong từ điển tương ứng với 2 ngôn ngữ được chọn
	 * @param itemSearch
	 * @param langSource
	 * @param langTarget
	 * @return
	 */
	public HashMap<String, String> GetListWord(String itemSearch, String langSource, String langTarget){
		HashMap<String, String> listWord = new HashMap<String, String>();
		String xmlFilePath = "F:\\Eclipse_Project\\MathDict\\WebContent\\Dictionary\\"+langSource+""+langTarget+".xml";
		try {
			File xmlFile = new File(xmlFilePath);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			NodeList nList = doc.getElementsByTagName("words");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					
					/*String item = String.valueOf(temp + 1)
							+ ". "
							+ eElement.getElementsByTagName("word").item(0).getTextContent();*/
					String word = eElement.getElementsByTagName("word").item(0).getTextContent();
					String mean = eElement.getElementsByTagName("mean").item(0).getTextContent();
					listWord.put(word, mean);
				}
			}
		} catch (Exception e) {
            e.printStackTrace();
        }
		return listWord;
    }
}
